using System;
using System.Runtime.Serialization;

namespace Metro.Admin.BusinessEntities
{
    [DataContract]
	public class Order 
    {
        private string id;
        private decimal originalPrice=0;
        private decimal dirtyPrice=0;
        private decimal amount=0;
        private decimal oldAmount = 0;
        private decimal commission=0;
        private string technicalName = string.Empty;
        private string userId = string.Empty;
        private string userName= string.Empty;
        private string customerId = string.Empty;
        private string customerName = string.Empty;
        private Market market;
        private int expirationNumber=0;
        private decimal opcfPrice=0;
        private decimal swap=0;
        private BusinessType businessType;
        private OrderClass orderClass;
        private DateTime date;
        private DateTime expirationDate;
        private OrderState state;
        private OrderType type;
        private Instruction orderInstruction = Instruction.Spot;  
		
		public Order()
        {

		}

        [DataMember]
        public String Id
        {
            get { return this.id;}
            set { this.id = value;}
        }
        
        [DataMember]
        public Decimal OriginalPrice
        {
            get { return this.originalPrice; }
            set { this.originalPrice = value; }
        }

        [DataMember]
        public Decimal DirtyPrice
        {
            get { return this.dirtyPrice + this.commission; }
            set { this.dirtyPrice = value; }
        }

        [DataMember]
        public Decimal Amount
        {
            get { return this.amount; }
            set { this.amount = value; }
        }

        [DataMember]
        public Decimal OldAmount
        {
            get { return this.oldAmount; }
            set { this.oldAmount = value; }
        }

        [DataMember]
        public Decimal Commission
        {
            get { return this.commission; }
            set { this.commission = value; }
        }

        [DataMember]
        public String TechnicalName
        {
            get { return this.technicalName; }
            set { this.technicalName = value; }
        }

        [DataMember]
        public String UserId
        {
            get { return this.userId; }
            set { this.userId = value; }
        }

        [DataMember]
        public String UserName
        {
            get { return this.userName; }
            set { this.userName = value; }
        }

        [DataMember]
        public String CustomerId
        {
            get { return this.customerId; }
            set { this.customerId = value; }
        }

        [DataMember]
        public String CustomerName
        {
            get { return this.customerName; }
            set { this.customerName = value; }
        }

        [DataMember]
        public Market Market
        {
            get { return this.market; }
            set { this.market = value; }
        }

        [DataMember]
        public String MarketString
        {
            get { return Enum.GetName(typeof(Market), this.market); }
            set { }
        }

        [DataMember]
        public Instruction OrderInstruction
        {
            get { return this.orderInstruction;}
            set { this.orderInstruction = value;}
        }

        [DataMember]
        public int ExpirationNumber
        {
            get { return this.expirationNumber; }
            set { this.expirationNumber = value; }
        }

        [DataMember]
        public Decimal OpcfPrice
        {
            get { return this.opcfPrice; }
            set { this.opcfPrice = value; }
        }

        [DataMember]
        public Decimal Swap
        {
            get { return this.swap; }
            set { this.swap = value; }
        }

        [DataMember]
        public BusinessType OrderBusinessType
        {
            get { return this.businessType; }
            set { this.businessType = value; }
        }

        [DataMember]
        public OrderClass OrderClass
        {
            get { return this.orderClass; }
            set { this.orderClass = value; }
        }

        [DataMember]
        public DateTime Date
        {
            get { return this.date; }
            set { this.date = value; }
        }

        [DataMember]
        public DateTime ExpirationDate
        {
            get { return this.expirationDate; }
            set { this.expirationDate = value; }
        }

        [DataMember]
        public OrderState State
        {
            get { return this.state; }
            set { this.state = value; }
        }

        [DataMember]
        public OrderType Type
        {
            get { return this.type; }
            set { this.type = value; }
        }

        [DataMember]
        public string BusinessTypeString
        {
            get { return Enum.GetName(typeof(BusinessType), this.businessType); }
            set { }
        }

        [DataMember]
        public string OrderClassString
        {
            get { return Enum.GetName(typeof(OrderClass), this.orderClass); }
            set { }
        }

        [DataMember]
        public string OrderStateString
        {
            get { return Enum.GetName(typeof(OrderState), this.state); }
            set { }
        }

        [DataMember]
        public string OrderTypeString
        {
            get { return Enum.GetName(typeof(OrderType), this.type); }
            set { }
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (this.GetType() != obj.GetType()) return false;
            
            Order order = (Order)obj;

            return this.id.Equals(order.id);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        } 
        
	}

    [DataContract(Name = "BusinessType")]
    public enum BusinessType
    {  
       [EnumMember] 
       Sell,
       [EnumMember]
       Buy
    }
    
    [DataContract(Name = "OrderClass")]
    public enum OrderClass
    {
       [EnumMember]
       Total,
       [EnumMember]
       Partial
    }

    [DataContract(Name = "OrderState")]
    public enum OrderState
    {
       [EnumMember]
       Active,
       [EnumMember]
       Cancel,
       [EnumMember]
       Pending
    }

    [DataContract(Name = "OrderType")]
    public enum OrderType
    {
       [EnumMember]
       Market,
       [EnumMember]
       Limit,
       [EnumMember]
       Stop
    }

    [DataContract(Name = "Market")]
    public enum Market
    {
        [EnumMember]
        Spot,
        [EnumMember]
        NextDay,
        [EnumMember]
        NoMarket
    }

    [DataContract(Name = "Instruction")]
    public enum Instruction
    {
        [EnumMember]
        Spot,
        [EnumMember]
        OPCF
    }

    [DataContract(Name = "OrderOperation")]
    public enum OrderOperation
    {
        [EnumMember]
        New,
        [EnumMember]
        Update,
        [EnumMember]
        Delete
    }


    public static class OrderServices
    {
        public static BusinessType Str2BusinessType(string businessType)
        {
            BusinessType retBusinessType = BusinessType.Sell;

            if (businessType.Equals("Sell"))
                retBusinessType = BusinessType.Sell;

            if (businessType.Equals("Buy"))
                retBusinessType = BusinessType.Buy;

            return retBusinessType;
        }

        public static OrderType Str2OrderType(string orderType)
        {
            OrderType retOrderType = OrderType.Market;

            if (orderType.Equals("Market"))
                retOrderType = OrderType.Market;

            if (orderType.Equals("Limit"))
                retOrderType = OrderType.Limit;

            if (orderType.Equals("Stop"))
                retOrderType = OrderType.Stop;

            return retOrderType;
        }

        public static OrderClass Str2OrderClass(string orderClass)
        {
            OrderClass retOrderClass = OrderClass.Total;

            if (orderClass.Equals("Total"))
                retOrderClass = OrderClass.Total;

            if (orderClass.Equals("Partial"))
                retOrderClass = OrderClass.Partial;

            return retOrderClass;
        }

        public static string OrderState2Str(OrderState state)
        {
            string stateStr = string.Empty;

            switch (state)
            { 
                case OrderState.Active:
                    stateStr = "A";
                    break;

                case OrderState.Cancel:
                    stateStr = "C";
                    break;

                case OrderState.Pending:
                    stateStr = "P";
                    break;
            }

            return stateStr;
        }

        public static string OrderType2Str(OrderType type)
        {
            string typeStr = string.Empty;

            switch (type)
            {
                case OrderType.Limit:
                    typeStr = "L";
                    break;

                case OrderType.Market:
                    typeStr = "M";
                    break;

                case OrderType.Stop:
                    typeStr = "S";
                    break;
            }

            return typeStr;
        }

        public static string BusinessType2Str(BusinessType type)
        {
            string businessStr = string.Empty;

            switch (type)
            {
                case BusinessType.Buy:
                    businessStr = "B";
                    break;

                case BusinessType.Sell:
                    businessStr = "S";
                    break;
            }

            return businessStr;
        }

        public static string OrderClass2Str(OrderClass orderClass)
        {
            string orderClassStr = string.Empty;

            switch (orderClass)
            {
                case OrderClass.Total:
                    orderClassStr = "T";
                    break;

                case OrderClass.Partial:
                    orderClassStr = "P";
                    break;
            }

            return orderClassStr;
        }

        public static string Market2Str(Market market)
        {
            string marketStr = string.Empty;

            switch (market)
            {
                case Market.Spot:
                    marketStr = "S";
                    break;

                case Market.NextDay:
                    marketStr = "N";
                    break;

                case Market.NoMarket:
                    marketStr = "X";
                    break;
            }

            return marketStr;
        }

        public static string Instruction2Str(Instruction instruction)
        {
            string instructionStr = string.Empty;

            switch (instruction)
            {
                case Instruction.Spot:
                    instructionStr = "S";
                    break;

                case Instruction.OPCF:
                    instructionStr = "O";
                    break;
            }

            return instructionStr;
        }

        public static BusinessType Letter2BusinessType(string businessType)
        {
            BusinessType retBusinessType = BusinessType.Sell;

            if (businessType.Equals("S"))
                retBusinessType = BusinessType.Sell;

            if (businessType.Equals("B"))
                retBusinessType = BusinessType.Buy;

            return retBusinessType;
        }

        public static OrderType Letter2OrderType(string orderType)
        {
            OrderType retOrderType = OrderType.Market;

            if (orderType.Equals("M"))
                retOrderType = OrderType.Market;

            if (orderType.Equals("L"))
                retOrderType = OrderType.Limit;

            if (orderType.Equals("S"))
                retOrderType = OrderType.Stop;

            return retOrderType;
        }

        public static OrderClass Letter2OrderClass(string orderClass)
        {
            OrderClass retOrderClass = OrderClass.Total;

            if (orderClass.Equals("T"))
                retOrderClass = OrderClass.Total;

            if (orderClass.Equals("P"))
                retOrderClass = OrderClass.Partial;

            return retOrderClass;
        }

        public static OrderState Letter2OrderState(string state)
        {
            OrderState retOrderState = OrderState.Active;

            if (state.Equals("A"))
                retOrderState = OrderState.Active;

            if (state.Equals("C"))
                retOrderState = OrderState.Cancel;

            if (state.Equals("P"))
                retOrderState = OrderState.Pending;

            return retOrderState;
        }

        public static Market Letter2Market(string market)
        {
            Market retMarket = Market.NoMarket;

            if (market.Equals("S"))
                retMarket = Market.Spot;

            if (market.Equals("N"))
                retMarket = Market.NextDay;

            if (market.Equals("X"))
                retMarket = Market.NoMarket;

            return retMarket;
        }

    }
}